package com.kj.platformlogistics.widget;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;
import androidx.core.content.PermissionChecker;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.kj.platformlogistics.CaptureActivity;
import com.kj.platformlogistics.R;
import com.kj.platformlogistics.base.BaseBottomDialog;
import com.kj.platformlogistics.constants.UrlConstants;
import com.kj.platformlogistics.home.activity.SolicilationedDetailActivity;
import com.kj.platformlogistics.home.adapter.SacnDetailAdapter;
import com.kj.platformlogistics.home.adapter.TraceAdapter;
import com.kj.platformlogistics.home.adapter.WaitSolicilationDetailAdapter;
import com.kj.platformlogistics.home.model.ScanModel;
import com.kj.platformlogistics.http.HttpRequest;
import com.kj.platformlogistics.http.HttpStringCallBack;
import com.kj.platformlogistics.util.JsonUtils;
import com.kj.platformlogistics.util.SPUtils;
import com.kj.platformlogistics.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;


public class ScanBottomDialog extends BaseBottomDialog {
    private static final int REQUEST_CODE = 101;
    private Context context;
   private ImageView ivBack;
   private TextView tvButton;
   private ScanModel model;
    private  SacnDetailAdapter adapter;
    private TextView tvPostName,tvPostAddress,tvReceiveName,tvReceiveAddress,tvNumber,
            tvOrderNumber,tvPayType,tvOrderCreateTime,tvCurrentNum,tvStatus,
            tvReceivePhone,tvPostPhone;
    private RecyclerView rcCommon;

    private ClickListenerInterface clickListenerInterface;

    public interface ClickListenerInterface {
        void funClick();
    }
//,String lat,String lng
    public ScanBottomDialog(Context context,ScanModel model) {
        this.context=context;
        this.model=model;
//        this.lat=lat;
//        this.lng=lng;
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        //获取到LayoutParams
        WindowManager.LayoutParams params = window.getAttributes();
        window.setSoftInputMode(params.SOFT_INPUT_ADJUST_RESIZE);
        //背景透明度
        params.dimAmount = getDimAmount();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        if (getHeight() > 0) {
            params.height = getHeight();
        } else {
            params.height = 1500;
        }
        params.gravity = Gravity.BOTTOM;
        //设置对话框位置的偏移量
        params.x = 0;
        params.y = 0;
        window.setAttributes(params);
    }

    @Override
    public int getLayoutRes() {
        return R.layout.scan_bottom_dialog;
    }

    @Override
    public void bindView(View v) {
        tvReceivePhone=v.findViewById(R.id.tvReceivePhone);
        tvReceiveName=v.findViewById(R.id.tvReceiveName);
        tvPayType=v.findViewById(R.id.tvPayType);
        tvReceiveAddress=v.findViewById(R.id.tvReceiveAddress);
        rcCommon=v.findViewById(R.id.rcCommon);
        tvPostAddress=v.findViewById(R.id.tvPostAddress);
        tvPostName=v.findViewById(R.id.tvPostName);
        tvOrderNumber=v.findViewById(R.id.tvOrderNumber);
        tvStatus=v.findViewById(R.id.tvStatus);
        tvOrderCreateTime=v.findViewById(R.id.tvOrderCreateTime);
        tvNumber=v.findViewById(R.id.tvNumber);
        tvCurrentNum=v.findViewById(R.id.tvCurrentNum);
        ivBack=v.findViewById(R.id.ivBack);
        tvPostPhone=v.findViewById(R.id.tvPostPhone);
        tvButton=v.findViewById(R.id.tvButton);
        ivBack.setOnClickListener(new clickListener());
        tvButton.setOnClickListener(new clickListener());

        if (model!=null){
            setView();
        }
    }


    public void setView(){
        tvPostName.setText(model.getResult().getMailName());
        tvPostAddress.setText(model.getResult().getMailAddress());
        tvReceiveName.setText(model.getResult().getReceiveName());
        tvReceiveAddress.setText(model.getResult().getReceiveAddress());
        tvNumber.setText(model.getResult().getNum()+"件");
        tvPayType.setText(model.getResult().getPayWay());
        tvOrderCreateTime.setText(model.getResult().getCreateTime());
        tvCurrentNum.setText(model.getResult().getScannedNum()+"件");
        tvPayType.setText(model.getResult().getPayWay());
        tvReceivePhone.setText(model.getResult().getReceivePhone());
        tvPostPhone.setText(model.getResult().getMailPhone());
        tvOrderNumber.setText(model.getResult().getWaybillNumber());
        if (model.getResult().getOrderDetailList().size()>0){
            adapter=new SacnDetailAdapter(model.getResult().getOrderDetailList());
            rcCommon.setAdapter(adapter);
            rcCommon.setLayoutManager(new LinearLayoutManager(context));
        }

        switch (model.getResult().getStatus()){
            case 0:
                tvStatus.setText("待揽件");
                if (model.getResult().getScannedNum()<model.getResult().getNum()){
                    tvButton.setText("继续扫描");
                }else {
                    tvButton.setText("确认揽收");
                }
                break;
            case 1:
                tvStatus.setText("已揽件");
                if (model.getResult().getScannedNum()<model.getResult().getNum()){
                    tvButton.setText("继续扫描");
                }else {
                    tvButton.setText("确认入库");
                }
                break;
            case 2:
                tvStatus.setText("待派送");
                if (model.getResult().getScannedNum()<model.getResult().getNum()){
                    tvButton.setText("继续扫描");
                }else {
                    if (model.getResult().getLogisticsStatusList()!=null&&model.getResult().getLogisticsStatusList().size()>0){
                        if (model.getResult().getLogisticsStatusList().get(model.getResult().getLogisticsStatusList().size()-1).getStatus()==3||
                                model.getResult().getLogisticsStatusList().get(model.getResult().getLogisticsStatusList().size()-1).getStatus()==4){
                            tvButton.setText("确认入库");
                        }else if(model.getResult().getLogisticsStatusList().get(model.getResult().getLogisticsStatusList().size()-1).getStatus()==5){
                            tvButton.setText("确认出库");
                        }
                    }
                }
                break;
            case 3:
                tvStatus.setText("派送中");
                if (model.getResult().getScannedNum()<model.getResult().getNum()){
                    tvButton.setText("继续扫描");
                }else {
                    tvButton.setText("确认签收");
                }
                break;
            case 4:
                tvStatus.setText("已完成");
                tvButton.setText("已签收");
                tvButton.setBackgroundResource(R.drawable.gray_bg_2);
                tvButton.setTextColor(context.getResources().getColor(R.color.white));
                break;
        }
    }

    public void setClicklistener(ClickListenerInterface clickListener) {
        clickListenerInterface = clickListener;
    }

    private class clickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ivBack:
                    dismiss();
                    break;
                case R.id.tvButton:
                    lat=SPUtils.getSharedStringData(context,"lat");
                    lng=SPUtils.getSharedStringData(context,"lng");
                    if (model.getResult().getStatus()==0){
                        //待揽收
                        if (model.getResult().getScannedNum()<model.getResult().getNum()){
                            clickListenerInterface.funClick();
                        }else {
                            confirmCollect(model.getResult().getWaybillNumber());
                        }
                    }else if (model.getResult().getStatus()==2){
                        //待派送
                        if (model.getResult().getScannedNum()<model.getResult().getNum()){
                            clickListenerInterface.funClick();
                        }else {
                            confirmCollect(model.getResult().getWaybillNumber());
                        }
                    }else if (model.getResult().getStatus()==3){
                        //派送中
                        if (model.getResult().getScannedNum()<model.getResult().getNum()){
                            clickListenerInterface.funClick();
                        }else {
                            confirmCollect(model.getResult().getWaybillNumber());
                        }
                    }else  if(model.getResult().getStatus()==1){
                        //已揽件
                        if (model.getResult().getScannedNum()<model.getResult().getNum()){
                            clickListenerInterface.funClick();
                        }else {
                            confirmCollect(model.getResult().getWaybillNumber());
                        }
                    }else {
                        Log.e("lat",lat+"::::::"+lng);
                    }
                    break;
                default:
                    break;
            }
        }
    }

    private String lng="";//纬度113.784208
    private String lat="";//经度34.758762

    //确认揽收/出库
    public void confirmCollect(String code){
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("waybillNumber",code);
            jsonObject.put("lat",lat);//纬度
            jsonObject.put("lng",lng);//经度
        } catch (JSONException e) {
            e.printStackTrace();
        }
        new HttpRequest(context).doPost(UrlConstants.logistics_verifyOrder, null,jsonObject,new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                try {
                    String data = result.toString();
                    //把请求得数据转换成json对象
                    JSONObject jsonObject = new JSONObject(data);
                    String message = jsonObject.getString("message");
                    Boolean success=jsonObject.getBoolean("success");
                    if(success){
                        dismiss();
                        Toast.makeText(context,"操作成功",Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) { }
        });
    }


}
